package com.yjz.jvm.juc.Lock;


import com.yjz.jvm.juc.Lock.ReentrantLockLearn.DecreaseThread;
import com.yjz.jvm.juc.Lock.ReentrantLockLearn.IncreaseThread;
import com.yjz.jvm.juc.Lock.ReentrantLockLearn.Target;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;


@Slf4j
public class Test {

    public static void main(String[] args) {

        Target t = new Target();
        Thread incease = new IncreaseThread(t, "increase");

        Thread decease = new DecreaseThread(t, "decease");

        decease.start();
        incease.start();
    }

    /**
     * 模拟主线程和子线程抢锁
     */
    @SneakyThrows
    @org.junit.Test
    public void testLock() {
        ReentrantLock lock = new ReentrantLock(true);

        lock.lock();
        try {
            Thread th = new Thread(() -> {
                lock.lock();
            });
            th.start();

            th.join();
        } finally {
            lock.unlock();
        }
    }
}
